#ifndef LOG_ASYNC_SINK_H
#define LOG_ASYNC_SINK_H

#include <log/sink.h>
#include <vector>
#include <util/async_pipe.h>
#include <base/defines.h>

namespace log {

class AsyncSink: public Sink {
    public:
        using Config = util::AsyncPipe::Config;

        void setConfig(const Config &cfg) { cfg_ = cfg; }
        void cleanup();

    protected:
        virtual void onEnable() override;
        virtual void onDisable() override;

        virtual void onLogFrontEnd(const LogContent *content) override;
        void onLogBackEndReadPipe(const void *data_ptr, size_t data_size);
        void onLogBackEnd(const LogContent *content);
        void updateTimestampStr(uint32_t sec);
        virtual void appendLog(const char *str, size_t len) = 0;
        virtual void flushLog() { }

    private:
        Config cfg_;
        util::AsyncPipe async_pipe_;
        bool is_pipe_inited_ = false;

        uint32_t timestamp_sec_ = 0;
        char timestamp_str_[TIMESTAMP_STRING_SIZE]; // 2023-08-20 15:26:30

        std::vector<char> buffer_; // 落盘前的统一缓冲
};

}

#endif